package org.kodejava.example.servlet; 
02. 
 
03.import java.io.BufferedReader; 
04.import java.io.IOException; 
05.import java.io.InputStream; 
06.import java.io.InputStreamReader; 
07.import java.io.PrintWriter; 
08. 
 
09.import javax.servlet.ServletContext; 
10.import javax.servlet.ServletException; 
11.import javax.servlet.http.HttpServlet; 
12.import javax.servlet.http.HttpServletRequest; 
13.import javax.servlet.http.HttpServletResponse; 
14. 
 
15.public class ReadTextFileServlet extends HttpServlet { 
16.    
protected void doGet(HttpServletRequest request, 
17.            
HttpServletResponse response) throws ServletException, IOException { 
18.         
 
19.        
response.setContentType("text/html"); 
20.         
 
21.        
// 
22.        
// We are going to read a file called configuration.properties. This 
23.        
// file is placed under the WEB-INF directory. 
24.        
// 
25.        
String filename = "/WEB-INF/configuration.properties"; 
26.         
 
27.        
ServletContext context = getServletContext(); 
28.         
 
29.        
// 
30.        
// First get the file InputStream using ServletContext.getResourceAsStream() 
31.        
// method. 
32.        
// 
33.        
InputStream is = context.getResourceAsStream(filename); 
34.        
if (is != null) { 
35.            
InputStreamReader isr = new InputStreamReader(is); 
36.            
BufferedReader reader = new BufferedReader(isr); 
37.            
PrintWriter writer = response.getWriter(); 
38.            
String text = ""; 
39.             
 
40.            
// 
41.            
// We read the file line by line and later will be displayed on the  
42.            
// browser page. 
43.            
// 
44.            
while ((text = reader.readLine()) != null) { 
45.                
writer.println(text); 
46.            
} 
47.        
} 
48.    
} 
49. 
 
50.    
protected void doPost(HttpServletRequest request, 
51.            
HttpServletResponse response) throws ServletException, IOException { 
52.    
} 
53.}